package dhz.tools.dialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.ResourceManager;

public class GzipDialog extends TitleAreaDialog {
	
	/**
	 * 如果新文件已经存在则提示“是否替换”
	 */
	public static final int TYPE_INQUIRE = 1;
	/**
	 * 如果新文件已经存在则直接替换
	 */
	public static final int TYPE_REPLACE = 2;
	/**
	 * 如果新文件已经存在则跳过
	 */
	public static final int TYPE_SKIP = 3;
	
	private Text gzip;
	private Button replaceBt;
	private Button skipBt;
	private Button inquireBt;
	private Text nameText;
	private Text sizeFrom;
	private Text sizeTo;
	
	private String gzipStr;
	private int type;
	private String[] nameTexts;
	private int sizeFromVal;
	private int sizeToVal;
	
	
	public GzipDialog(Shell parentShell) {
		super(parentShell);
	}
	
	protected Control createDialogArea(Composite parent) {
		setMessage("\u8BF7\u8BBE\u7F6EGZIP\u7684\u538B\u7F29\u53C2\u6570\u3002");
		setTitle("\u538B\u7F29\u53C2\u6570\u914D\u7F6E");
		setTitleImage(ResourceManager.getPluginImage("dhz-tools", "icons/gzip-title.png"));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		GridLayout gl_container = new GridLayout(2, false);
		gl_container.marginTop = 5;
		container.setLayout(gl_container);
		GridData gd_container = new GridData(GridData.FILL_BOTH);
		gd_container.widthHint = 220;
		container.setLayoutData(gd_container);
		
		Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("\u538B\u7F29\u6269\u5C55\u540D\uFF1A");
		
		gzip = new Text(container, SWT.BORDER);
		gzip.setToolTipText("\u53EA\u80FD\u662F\u6570\u5B57\u3001\u5B57\u6BCD\u3001\u4E0B\u5212\u7EBF\u7684\u7EC4\u5408\u3002");
		gzip.setText("gzip");
		gzip.setTextLimit(10);
		gzip.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(container, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("\u5DF2\u5B58\u5728\u5904\u7406\uFF1A");
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		RowLayout rl_composite = new RowLayout(SWT.HORIZONTAL);
		rl_composite.fill = true;
		composite.setLayout(rl_composite);
		
		skipBt = new Button(composite, SWT.RADIO);
		skipBt.setSelection(true);
		skipBt.setText("\u8DF3\u8FC7");
		
		replaceBt = new Button(composite, SWT.RADIO);
		replaceBt.setText("\u8986\u76D6");
		
		inquireBt = new Button(composite, SWT.RADIO);
		inquireBt.setText("\u63D0\u793A");
		
		Label label_3 = new Label(container, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText("\u6269\u5C55\u540D\uFF1A");
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(40);
		nameText.setToolTipText("\u6307\u5B9A\u9700\u8981\u538B\u7F29\u7684\u6587\u4EF6\u6269\u5C55\u540D\uFF0C\u591A\u4E2A\u95F4\u7528\u9017\u53F7\u9694\u5F00\uFF0C\u7A7A\u8868\u793A\u4E0D\u9650\u5236\u3002");
		GridData gd_txtJs = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_txtJs.widthHint = 85;
		nameText.setLayoutData(gd_txtJs);
		
		Label label_4 = new Label(container, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText("\u5927\u5C0F\uFF1A");
		
		Composite composite_1 = new Composite(container, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		composite_1.setLayout(new GridLayout(4, false));
		
		Label label_5 = new Label(composite_1, SWT.NONE);
		label_5.setText("\u4ECE");
		
		sizeFrom = new Text(composite_1, SWT.BORDER);
		sizeFrom.setTextLimit(10);
		sizeFrom.setToolTipText("\u6587\u4EF6\u5927\u5C0F\u5B57\u8282\u6570\uFF0C0\u8868\u793A\u4E0D\u9650\u5236\u3002");
		GridData gd_text = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text.widthHint = 65;
		sizeFrom.setLayoutData(gd_text);
		sizeFrom.setText("0");
		
		Label label_6 = new Label(composite_1, SWT.NONE);
		label_6.setText("\u5230");
		
		sizeTo = new Text(composite_1, SWT.BORDER);
		sizeTo.setTextLimit(10);
		sizeTo.setToolTipText("\u6587\u4EF6\u5927\u5C0F\u5B57\u8282\u6570\uFF0C0\u8868\u793A\u4E0D\u9650\u5236\u3002");
		GridData gd_text_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_text_2.widthHint = 65;
		sizeTo.setLayoutData(gd_text_2);
		sizeTo.setText("0");

		return area;
	}

	protected void createButtonsForButtonBar(Composite parent) {
		Button button = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,true);
		button.setText("\u786E\u5B9A");
		Button button_1 = createButton(parent, IDialogConstants.CANCEL_ID,IDialogConstants.CANCEL_LABEL, false);
		button_1.setText("\u53D6\u6D88");
	}

	protected void okPressed() {
		String _gzip = gzip.getText().trim();
		if(!_gzip.matches("[\\w]+")){
			MessageDialog.openError(this.getParentShell(),"错误","扩展名只能是数字、字母及下划线的组合！");
			return;
		}
		String _nameText = nameText.getText().trim();
		if(_nameText.length() > 0 && !_nameText.matches("[\\w,]+")){
			MessageDialog.openError(this.getParentShell(),"错误","过滤的文件类型只能是数字、字母及下划线的组合！");
			return;
		}
		try {
			sizeFromVal = Integer.parseInt(sizeFrom.getText().trim());
			sizeToVal = Integer.parseInt(sizeTo.getText().trim());
		} catch (NumberFormatException e) {
			MessageDialog.openError(this.getParentShell(),"错误","文件大小！");
			return;
		}
		
		gzipStr = _gzip;
		nameTexts = _nameText.split(",");
		if(replaceBt.getSelection()){
			type = TYPE_REPLACE;
		}else if(skipBt.getSelection()){
			type = TYPE_SKIP;
		}else if(inquireBt.getSelection()){
			type = TYPE_INQUIRE;
		}
		super.okPressed();
	}

	@Override
	protected void configureShell(Shell newShell) {
		newShell.setText("GZIP压缩");
		super.configureShell(newShell);
	}
	
	protected Point getInitialSize() {
		return new Point(291, 253);
	}

	public String getGzipStr() {
		return gzipStr;
	}

	public int getType() {
		return type;
	}

	public String[] getNameTexts() {
		return nameTexts;
	}

	public int getSizeFromVal() {
		return sizeFromVal;
	}

	public int getSizeToVal() {
		return sizeToVal;
	}

}
